<template>
	<div>
		<el-select v-model="province.id" placeholder="请选择省份" :disabled="isDisabled" @change="getCityList" >
	   		<el-option v-for="item in provinceList" :key="item.nid" :label="item.name" :value="item.nid" :data-name="item.name" :ref="item.nid" ></el-option>				     
	    </el-select>
	    <el-select v-model="city.id" placeholder="请选择城市" :disabled="isDisabled" @change="getCountyList" >
	    	<el-option v-for="item in cityList" :key="item.nid" :label="item.name" :value="item.nid" :data-name="item.name" :ref="item.nid"></el-option>
	    </el-select>
	    <el-select v-show="!link" v-model="county.id" placeholder="请选择区（县）" :disabled="isDisabled||noList" @change="changeCounty" >
	    	<el-option v-for="(item,index) in countyList" :key="index" :label="item.name" :value="item.nid" :data-name="item.name" :ref="item.nid"></el-option>
	    </el-select>	 
	</div>
</template>
<script>
    import agentHttp from '@/utils/agentHttp.js'
    export default{
		created(){
   			this.getProvinceList();	      
        },
        mounted(){
        	   this.province.id=this.provinceNid||""
        	   this.city.id=this.cityNid||""
        	   this.county.id=this.countyNid||""
        	   if(this.noCounty){
        	   	  this.link=this.noCounty
        	   }
        	   if(!this.provinceNid||this.provinceNid=="undefined"){
        	   	  this.countyReload=true;
        	   	  this.cityReload=true;
        	   }
        },
		props:['isDisabled','provinceNid','cityNid','countyNid','noCounty'],
		data(){			
			return{
				province:{id:"",name:""},
				city:{id:"",name:""},
				county:{id:"",name:""},
		        provinceList:"",
		        cityList:"",
		        countyList:"",
		        cityReload:false,
		        countyReload:false,
		        link:false,
		        noList:false
			}
		},
		methods:{
		    //获取省份
		    getProvinceList(){
		      	agentHttp.get('adminApi/admin/dictionary/get_province_list' ).then((data)=>{
		      		if(data.success){  
			            this.provinceList = data.data;
			        }		        	
		      	})  
		    },
		    //获取城市
		    getCityList(){
		    	if (this.province.id != '' || this.province.id != null) {
		    		agentHttp.get('adminApi/admin/dictionary/find_city_new/'+this.province.id).then((data)=>{
			            if(data.success){  
			            	this.cityList = data.data;
			            	if (this.cityReload) {
			            		if(data.data.length>0){
				            		this.city.id = data.data[0].nid;			            		
				            		this.$nextTick( ()=> {
				            			if(this.$refs[this.province.id])
				            		    this.province.name=this.$refs[this.province.id][0].$attrs['data-name'];
				            		    if(this.$refs[this.city.id])
					            		this.city.name=this.$refs[this.city.id][0].$attrs['data-name'];
				            			//this.$emit('receive-city-data',this.province,this.city,this.county);
				            	    })
				            	}else{
				            		this.city.id="";
				            		this.city.name="";
				            	}
			            	}
			            	this.cityReload = true;			            	
			            } 
			        })
		    	}
		    },
		    //获取区（县）
		    getCountyList(){
		    	if (this.city.id != '' || this.city.id != null) {
		    		agentHttp.get('adminApi/admin/dictionary/find_county_new/'+this.city.id).then((data)=>{
			            if(data.success){  
							this.countyList = data.data;
							
			            	if (this.countyReload) {
                                if(data.data.length>0){
				            		this.county.id = data.data[0].nid;
				            		this.noList=false;
				            		this.$nextTick( ()=> {
				            			if(this.$refs[this.city.id])
					            		this.city.name=this.$refs[this.city.id][0].$attrs['data-name'];
					            	    if (this.$refs[this.county.id])
							    		this.county.name=this.$refs[this.county.id][0].$attrs['data-name'];
				            			//this.$emit('receive-city-data',this.province,this.city,this.county);
				            		})
			            		}else{
				            		this.county.id = "";
				            		this.county.name = "";
									this.noList=true;
									//存在只存在省市的情况，changeCounty不会执行，所以需要发射一次id
									this.$emit('receive-city-data',this.province,this.city,this.county);
				            	}
							}
							

			            	this.countyReload = true;			            	
			            } 
			        })
		    	}
		    },
		    changeCounty(){
		    	this.$nextTick( ()=> {
		    		if (this.$refs[this.county.id])
		    		this.county.name=this.$refs[this.county.id][0].$attrs['data-name'];
		    		this.$emit('receive-city-data',this.province,this.city,this.county);		    		
		    	})		    	
		    }
		}
	};	
</script>
